<?php

namespace SDXApi\Repositories;
use SDXApi\Model\GoodsModel;
use SDXApi\Model\GoodspreselltimeModel;
use SDXApi\Model\GoodsstorageModel;
use SDXApi\Model\GoodsWidModel;
use SDXApi\Model\OrderinfoModel;
use SDXApi\Model\SeminarTemplateCategoryGoodsModel;
use SDXApi\Service\PromoteService;

/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/28
 * Time: 9:47
 */
class GoodsRepository
{
	/**
	 * @var GoodsWidModel
	 */
	protected $goodsWidModel;

	/**
	 * @var
	 */
	public $userId;

	/**
	 * @var
	 */
	public $wid;

	/**
	 * @var GoodsstorageModel
	 */
	protected $goodsStorageModel;

	/**
	 * @var GoodsModel
	 */
	protected $goodsModel;

	/**
	 * @var BuyNowGoodsRepository
	 */
	protected $buyNowGoodsRepository;

	/**
	 * @var GoodspreselltimeModel
	 */
	protected $goodsPresellTimeModel;

	/**
	 * @var SeminarTemplateCategoryGoodsModel
	 */
	protected $seminarCategoryGoodsModel;

    /**
     * @var
     */
    protected $orderInfoModel;

	/**
	 * 注入实例
	 * GoodsRepository constructor.
	 * @param $userId
	 * @param $wid
	 */
	public function __construct($userId = 0, $wid = 8)
	{
		$this->goodsWidModel = new GoodsWidModel();
		$this->goodsStorageModel = new GoodsstorageModel();
		$this->goodsModel = new GoodsModel();
		$this->buyNowGoodsRepository = new BuyNowGoodsRepository($userId, $wid);
		$this->goodsPresellTimeModel = new GoodspreselltimeModel();
		$this->seminarCategoryGoodsModel = new SeminarTemplateCategoryGoodsModel();
		$this->orderInfoModel = new OrderinfoModel();

		$userId = empty($userId) ? 0 : $userId;
		$wid = empty($wid) ? 8 : $wid;

		$this->userId = $userId;
		$this->wid = $wid;
	}


	/**
	 * 处理商品数据(已弃用)
	 * Created by Jerry
	 * @param $goods
	 * @return mixed
	 */
	public function formatGoodsByTemplate($goods)
	{
		foreach ($goods as $key => $value){

			$goodsInfo = $this->findGoodsInfoById($value['goods_id']);

			if (empty($goodsInfo)) {
				unset($goods[$key]);
				continue;
			}

			$goodsNumber = $this->findGoodsStorageById($value['goods_id']);

			$goods[$key]['spec_unit'] = $goodsInfo['spec_unit'];
			$goods[$key]['spec_value'] = $goodsInfo['spec_value'];
			$goods[$key]['min_buy_num'] = $goodsInfo['min_buy_num'];
			$goods[$key]['goods_name'] = $goodsInfo['goods_name'];
			$goods[$key]['original_img'] = img_init($goodsInfo['original_img']);
			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

			if($goodsInfo['goods_img']){
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_img']);
			}else{
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_thumb']);
			}
			$goods[$key]['goods_img'] =  img_init($goodsInfo['goods_img']);

			$goods[$key]['goods_brief'] = $goodsInfo['goods_brief'];
			$goods[$key]['is_presell'] = ($goodsInfo['is_presell'] == 1) ? $this->goodsStorageModel->check_whether_presell($value['goods_id'], $this->wid) : $goodsInfo['is_presell'];

			if ($goodsInfo['is_presell'] == 1) {
//				预售商品使用虚拟库存
				$goods[$key]['goods_number'] = $goodsInfo['goods_number'];
			}
			else
			{
				$practical_num = $goodsNumber - C('rest_goods_number');

				if ($practical_num <= 0) {
					$practical_num = 0;
				}

//				现售商品使用实际库存
				$goods[$key]['goods_number'] = $practical_num;
			}

			if ($goods[$key]['is_presell']) {
				if ($goodsInfo['delivery_type'] == 1) {
					if ($this->goodsPresellTimeModel->check_shipping_goodspreselltime($value['goods_id'])) {
						$time = time();
						$shippingTime = $this->goodsPresellTimeModel->check_shippingtime($value['goods_id'], $time);

						if ($shippingTime) {
							$goods[$key]['preselltimeres'] = $shippingTime;
						}
						else{
							$goods[$key]['preselltimeres'] = array(
								'delivery_time'	=>	'',
							);
						}
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else if($goodsInfo['delivery_type'] == 2) {
					if ($goodsInfo['delivery_cycle']>=0) {
						$gmd = local_date('Y-m-d', gmtime());

						$goodsPresellTime = local_date('Y-m-d H:i:s', local_strtotime($gmd . ' +' . $goodsInfo['delivery_cycle'] . ' day ' . C('DEFAULT_PRESELL_TIME_H')));
						$goods[$key]['preselltimeres'] = array(
							'delivery_time' => $goodsPresellTime
						);
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else{
					$goods[$key]['preselltimeres'] = array(
						'delivery_time'	=>	'',
					);
				}
			}
			else{
				$goodsStorage = $this->goodsStorageModel->get_warehous_goods($this->wid, $value['goods_id']);
				if ($goodsStorage) {
					$goods[$key]['goods_number'] = $goodsStorage['practical_num'];
				}
				else{
					$goods[$key]['goods_number'] = 0;
				}
				$goods[$key]['preselltimeres'] = array('delivery_time' => '');
			}

			$goods[$key]['warn_number'] = $goodsInfo['warn_number'];

			if ($goods[$key]['goods_number'] == 0) {
				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
			}

//			else if ($goodsInfo['is_presell'] == 1 && $goodsInfo['goods_number'] <= 0) {
//				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
//			}

//			登录已未登录商品价格
			if ($this->userId) {
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price($value['goods_id'], $this->userId);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}
			else
			{
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}

//			促销价处理
			$price = bargain_price($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
			if ($goodsInfo['is_buynow'] == 0) {
				$goods[$key]['promote_end_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_end_date']));
				$goods[$key]['promote_start_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_start_date']));
				$goods[$key]['promote_price'] = $price;
			}
//			else
//			{
//				$goods[$key]['promote_price'] = 0;
//				$goods[$key]['promote_end_date'] = '';
//				$goods[$key]['promote_start_date'] = '';
//			}

			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

//			秒杀
			if ($goodsInfo['is_buynow'] == 1) {
				$goods[$key]['promote_end_date'] = $goodsInfo['promote_end_date'];
				$goods[$key]['promote_start_date'] = $goodsInfo['promote_start_date'];
				$goods[$key]['promote_goods_number'] = $goodsInfo['promote_goods_number'];
				$goods[$key]['promote_today_number'] = $goodsInfo['promote_today_number'];
				$goods[$key]['promote_price'] = $price;

				$goods[$key] = $this->buyNowGoodsRepository->getBuyNowGoodsData($goods[$key]);
			}

		}

		return $goods;

	}

	/**
	 * 处理商品库存
	 * Created by Jerry
	 * @param array $goods
	 * @return array
	 */
	public function checkGoodsPractical($goods = array())
	{
		if (! empty($goods) && is_array($goods)) {
			foreach ($goods as $key => $value) {

				if ($value['is_presell'] == 1) {
					$goods[$key]['is_presell'] = ($value['practical_num'] > 0) ? 0 : 1;
				}

				if ($goods[$key]['is_presell'] == 0) {

//					获取商品库存
					$goodsStorage = $this->goodsStorageModel->get_warehous_goods($this->wid, $value['goods_id']);
					$goodsNumber = $goodsStorage['practical_num'];
					$practical_num = $goodsNumber - C('rest_goods_number');

					if ($practical_num <= 0) {
						$practical_num = 0;
					}

//					现售商品使用实际库存
					$goods[$key]['goods_number'] = $practical_num;
				}

//				到货通知
				if ($goods[$key]['goods_number'] == 0) {
					//$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
                    $goods[$key]['is_book_goods'] = 0;
				}
			}
		}

		return $goods;
	}

    /**
     * Created by Jerry
     * @param array $goods
     * @return array|bool
     */
	public function formatGoodsArticleTransform($goods = array())
    {
        if (empty($goods)) {
            return false;
        }

        if ($goods['is_presell'] == 1) {
            $goods['is_presell'] = ($goods['practical_num'] > 0) ? 0 : 1;
        }

        if ($goods['is_presell'] == 0) {
            $practical_num = $goods['practical_num'] - C('rest_goods_number');

            if ($practical_num <= 0) {
                $practical_num = 0;
            }

//				现售商品使用实际库存
            $goods['goods_number'] = $practical_num;
        }

        $goods['preselltimeres'] = array(
            'delivery_time'	=>	''
        );

        if ($goods['is_presell']) {
            if ($goods['delivery_type'] == 1) {
                $shipingTime = $this->goodsPresellTimeModel->check_shipping_goodspreselltime($goods['goods_id']);
                if ($shipingTime) {
                    $time = time();
                    $shippingTime = $this->goodsPresellTimeModel->check_shippingtime($goods['goods_id'], $time);

                    if ($shippingTime) {
                        $goods['preselltimeres'] = $shippingTime;
                    }
                }
            }
            else if($goods['delivery_type'] == 2) {
                if ($goods['delivery_cycle']>=0) {
                    $gmd = local_date('Y-m-d', gmtime());

                    $goodsPresellTime = local_date('Y-m-d H:i:s', local_strtotime($gmd . ' +' . $goods['delivery_cycle'] . ' day ' . C('DEFAULT_PRESELL_TIME_H')));
                    $goods['preselltimeres'] = array(
                        'delivery_time' => $goodsPresellTime
                    );
                }
            }
        }

        if ($goods['goods_number'] == 0) {
            $goods['is_book_goods'] = $this->goodsBook($goods['goods_id']);
        }


//			登录已未登录商品价格
        if ($this->userId) {
            $goods['market_price'] = $this->goodsModel->get_vip_price_for_all($goods['goods_id'], 1);
            $goods['vip_price'] = $this->goodsModel->get_vip_price($goods['goods_id'], $this->userId);
            $goods['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($goods['goods_id']);
        }
        else
        {
            $goods['market_price'] = $this->goodsModel->get_vip_price_for_all($goods['goods_id'], 1);
            $goods['vip_price'] = $this->goodsModel->get_vip_price_for_all($goods['goods_id'], 1);
            $goods['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($goods['goods_id']);
        }

        return $goods;

    }


    /**
	 * 处理商品数据
	 * Created by Jerry
	 * @param $goods
	 * @return bool|array
	 */
	public function formatGoodsTransform($goods)
	{
		if (empty($goods)) {
			return false;
		}

		foreach ($goods as $key => $value){

			if ($value['is_presell'] == 1) {
				$goods[$key]['is_presell'] = ($value['practical_num'] > 0) ? 0 : 1;
			}

			if ($goods[$key]['is_presell'] == 0) {
				$practical_num = (int) $value['practical_num'] - C('rest_goods_number');

				if ($practical_num <= 0) {
					$practical_num = 0;
				}

//				现售商品使用实际库存
				$goods[$key]['goods_number'] = (int) $practical_num;
			}

			$goods[$key]['preselltimeres'] = array(
				'delivery_time'	=>	''
			);

			$goods[$key]['goods_img'] = $value['original_img'];

			if ($goods[$key]['is_presell']) {
				if ($value['delivery_type'] == 1) {
					$shipingTime = $this->goodsPresellTimeModel->check_shipping_goodspreselltime($value['goods_id']);
					if ($shipingTime) {
						$time = time();
						$shippingTime = $this->goodsPresellTimeModel->check_shippingtime($value['goods_id'], $time);

						if ($shippingTime) {
							$goods[$key]['preselltimeres'] = $shippingTime;
						}
					}
				}
				else if($value['delivery_type'] == 2) {
					if ($value['delivery_cycle']>=0) {
						$gmd = local_date('Y-m-d', gmtime());

						$goodsPresellTime = local_date('Y-m-d H:i:s', local_strtotime($gmd . ' +' . $value['delivery_cycle'] . ' day ' . C('DEFAULT_PRESELL_TIME_H')));
						$goods[$key]['preselltimeres'] = array(
							'delivery_time' => $goodsPresellTime
						);
					}
				}
			}

			$goods[$key]['warn_number'] = $value['warn_number'];

			if ($goods[$key]['goods_number'] == 0) {
				//$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
			}else{
                //$goods[$key]['is_book_goods'] = 0;
            }

            $isBuy = $this->isBuyGoodsByUserId($value['goods_id']);
            //$goods[$key]['is_buy'] = $isBuy == true ? '1' : '0';
            $goods[$key]['is_buy'] = 0;

//			登录已未登录商品价格
			if ($this->userId) {
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price($value['goods_id'], $this->userId);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}
			else
			{
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}

//			促销价处理
//			$price = bargain_price($value['promote_price'], $value['promote_start_date'], $value['promote_end_date']);
            $promoteService = new PromoteService();
			$isPromote = $promoteService->getPromotePriceStatus($value);
            if ($isPromote === true) {
                $price = bargain_price($value['promote_price'], $value['promote_start_date'], $value['promote_end_date']);
                $goods[$key]['promote_end_date'] = date('Y-m-d H:i:s', localtotime($value['promote_end_date']));
                $goods[$key]['promote_start_date'] = date('Y-m-d H:i:s', localtotime($value['promote_start_date']));
                $goods[$key]['promote_price'] = $price;
                $goods[$key]['promote_message'] = $promoteService->getPromoteGoodsNumber($value)->getNumberMessage();
            }
            else{
                $goods[$key]['promote_end_date'] = '';
                $goods[$key]['promote_start_date'] = '';
                $goods[$key]['promote_price'] = 0;
                $goods[$key]['promote_message'] = '';
            }

//			$goods[$key]['is_buynow'] = $value['is_buynow'];

//			秒杀
			if ($value['is_buynow'] == 1) {
				$goods[$key]['promote_end_date'] = $value['promote_end_date'];
				$goods[$key]['promote_start_date'] = $value['promote_start_date'];
				$goods[$key]['promote_goods_number'] = $value['promote_goods_number'];
				$goods[$key]['promote_today_number'] = $value['promote_today_number'];
				$goods[$key]['promote_price'] = $price;

				$goods[$key] = $this->buyNowGoodsRepository->getBuyNowGoodsData($goods[$key]);
			}

		}

		return $goods;

	}

    /**
     * 判断是否已购买过此商品
     * @author Jerry
     * @param int $goodsId
     * @return bool
     */
    public function isBuyGoodsByUserId($goodsId = 0)
    {
        if (empty($goodsId)) {
            return false;
        }

        $key = 'user_is_buy_goods_'.$goodsId.'_'.$this->userId;

        $count = S($key);

        if ($this->userId == 73248) {
            //print_r($key);exit;
        }

        if ($count === false) {
            $count = $this->orderInfoModel->isBuyGoodsByUserIdAndGoodsId($this->userId, $goodsId);
            if (! empty($count)) {
                S($key, $count, 60*60*24);
            } else {
                S($key, $count, 60*5);
            }
        }

        if (! empty($count)) {
            return true;
        }

        return false;
    }


    /**
     * 过滤无库存商品
     * @author Jerry
     * @param array $goods
     * @return array
     */
    public function filterGoodsNumber($goods = array())
    {
        if (empty($goods)) {
            return array();
        }

        $data = array();
        foreach ($goods as $key => $value) {
            if ($value['practical_num'] > 0) {
                $data[] = $value;
            }
        }

        return $data;
    }



	/**
	 * 处理分类商品数据（已弃用）
	 * Created by Jerry
	 * @param $goods
	 * @return mixed
	 */
	public function formatCategoryGoodsByTemplate($goods)
	{
		foreach ($goods as $key => $value){

			$goodsInfo = $this->findGoodsInfoById($value['goods_id']);

			if (empty($goodsInfo)) {
				unset($goods[$key]);
				continue;
			}

			$goodsNumber = $this->findGoodsStorageById($value['goods_id']);

			$goods[$key]['spec_unit'] = $goodsInfo['spec_unit'];
			$goods[$key]['spec_value'] = $goodsInfo['spec_value'];
			$goods[$key]['min_buy_num'] = $goodsInfo['min_buy_num'];
			$goods[$key]['goods_name'] = $goodsInfo['goods_name'];
			$goods[$key]['original_img'] = img_init($goodsInfo['original_img']);
			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

			if($goodsInfo['goods_img']){
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_img']);
			}else{
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_thumb']);
			}
			$goods[$key]['goods_img'] =  img_init($goodsInfo['goods_img']);

			$goods[$key]['goods_brief'] = $goodsInfo['goods_brief'];
			$goods[$key]['is_presell'] = ($goodsInfo['is_presell'] == 1) ? $this->goodsStorageModel->check_whether_presell($value['goods_id'], $this->wid) : $goodsInfo['is_presell'];

			if ($goodsInfo['is_presell'] == 1) {
//				预售商品使用虚拟库存
				$goods[$key]['goods_number'] = $goodsInfo['goods_number'];
			}
			else
			{
//				现售商品使用实际库存
				$goods[$key]['goods_number'] = $goodsNumber;
			}

			if ($goods[$key]['is_presell']) {
				if ($goodsInfo['delivery_type'] == 1) {
					if ($this->goodsPresellTimeModel->check_shipping_goodspreselltime($value['goods_id'])) {
						$time = time();
						$shippingTime = $this->goodsPresellTimeModel->check_shippingtime($value['goods_id'], $time);

						if ($shippingTime) {
							$goods[$key]['preselltimeres'] = $shippingTime;
						}
						else{
							$goods[$key]['preselltimeres'] = array(
								'delivery_time'	=>	'',
							);
						}
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else if($goodsInfo['delivery_type'] == 2) {
					if ($goodsInfo['delivery_cycle']>=0) {
						$gmd = local_date('Y-m-d', gmtime());

						$goodsPresellTime = local_date('Y-m-d H:i:s', local_strtotime($gmd . ' +' . $goodsInfo['delivery_cycle'] . ' day ' . C('DEFAULT_PRESELL_TIME_H')));
						$goods[$key]['preselltimeres'] = array(
							'delivery_time' => $goodsPresellTime
						);
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else{
					$goods[$key]['preselltimeres'] = array(
						'delivery_time'	=>	'',
					);
				}
			}
			else{
				$goodsStorage = $this->goodsStorageModel->get_warehous_goods($this->wid, $value['goods_id']);
				if ($goodsStorage) {
					$goods[$key]['goods_number'] = $goodsStorage['practical_num'];
				}
				else{
					$goods[$key]['goods_number'] = 0;
				}
				$goods[$key]['preselltimeres'] = array('delivery_time' => '');
			}

			$goods[$key]['warn_number'] = $goodsInfo['warn_number'];

			if ($goods[$key]['goods_number'] == 0) {
				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
			}

//			else if ($goodsInfo['is_presell'] == 1 && $goodsInfo['goods_number'] <= 0) {
//				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
//			}

//			登录已未登录商品价格
			if ($this->userId) {
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price($value['goods_id'], $this->userId);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}
			else
			{
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}

//			促销价处理
			$price = bargain_price($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
			if ($goodsInfo['is_buynow'] == 0 && $price > 0) {
				$goods[$key]['promote_end_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_end_date']));
				$goods[$key]['promote_start_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_start_date']));
				$goods[$key]['promote_price'] = $price;
			}

			if ($goodsInfo['is_buynow'] == 0 && $price == 0) {
				$goods[$key]['promote_end_date'] = 0;
				$goods[$key]['promote_start_date'] = 0;
				$goods[$key]['promote_price'] = $price;
			}



			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

//			秒杀
			if ($goodsInfo['is_buynow'] == 1) {

				$goods[$key]['promote_end_date'] = $goodsInfo['promote_end_date'];
				$goods[$key]['promote_start_date'] = $goodsInfo['promote_start_date'];
				$goods[$key]['promote_goods_number'] = $goodsInfo['promote_goods_number'];
				$goods[$key]['promote_today_number'] = $goodsInfo['promote_today_number'];
				$goods[$key]['promote_price'] = $goodsInfo['promote_price'];

				$goods[$key] = $this->buyNowGoodsRepository->getBuyNowGoodsData($goods[$key]);
			}

		}

		return $goods;

	}

    /**
     * @author Jerry
     * @param $category_id
     * @param $goods_where
     * @param $wid
     * @param int $is_hide_promote
     * @return bool|mixed
     */
	public function findCategoryGoodsByCategoryId($category_id = 0, $goods_where = array(), $wid = 8, $is_hide_promote = 0)
	{
		$goods =  $this->seminarCategoryGoodsModel->findCategoryGoodsByCategoryId($category_id, $goods_where, $wid, $is_hide_promote);
		return $goods;
	}

	/**
	 * 返回商品详情
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	protected function findGoodsInfoById($id)
	{
		return $this->goodsWidModel->setTableNameByWid($this->wid)->get_goods_info_by_goods_id($id);
	}

	/**
	 * 返回指定id库存数量
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	protected function findGoodsStorageById($id)
	{
		$data = $this->goodsStorageModel->get_warehous_goods($this->wid, $id);

		return $data['practical_num'] + 0;
	}

	/**
	 * 返回订阅数据
	 * Created by Jerry
	 * @param $goodsId
	 * @return int
	 */
	public function goodsBook($goodsId)
	{
        $key = 'user_is_book_goods_'.$goodsId.'_'.$this->userId;

        $data = S($key);

        if ($data === false) {
            $rs = $this->goodsModel->is_book_goods($goodsId, $this->userId, 0);

            if ($rs) {
                $data = 1;
                S($key, $data, 60*60*24);
            } else {
                $data = 0;
                S($key, $data, 60*5);
            }
        }

        return $data;
	}

	/**
	 * 动态设置变量
	 * Created by Jerry
	 * @param $name
	 * @param $value
	 * @return $this
	 */
	protected function __set($name, $value)
	{
		$this->$name = $value;
		return $this;
	}
}